package com.fish.admin.module.system.manager;

import com.fish.admin.module.system.domain.entity.SysAuthority;
import com.fish.admin.module.system.enums.AuthorityTypeEnum;
import com.fish.admin.module.system.enums.SubjectTypeEnum;
import com.fish.admin.module.system.mapper.SysAuthorityMapper;
import com.fish.common.mybatisplus.query.LambdaQueryWrapperX;
import com.fish.common.mybatisplus.query.MPJLambdaWrapperX;
import com.fish.common.support.util.EnumUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class SysAuthorityManager {
    private final SysAuthorityMapper sysAuthorityMapper;

    public void batchSave(Long id, SubjectTypeEnum subjectTypeEnum, AuthorityTypeEnum authorityTypeEnum, List<Long> menuIds) {
        List<SysAuthority> sysAuthorityList = new ArrayList<>(menuIds.size());
        menuIds.stream().forEach(menuId -> {
            SysAuthority sysAuthority = new SysAuthority();
            sysAuthority.setSubjectId(id);
            sysAuthority.setSubjectType(subjectTypeEnum.getValue());
            sysAuthority.setAuthorityId(menuId);
            sysAuthority.setAuthorityType(authorityTypeEnum.getValue());
            sysAuthorityList.add(sysAuthority);
        });
        sysAuthorityMapper.insertBatchSomeColumn(sysAuthorityList);
    }


    public void delete(Long id, SubjectTypeEnum subjectType, AuthorityTypeEnum authorityType) {
        LambdaQueryWrapperX<SysAuthority> queryWrapper = new LambdaQueryWrapperX<SysAuthority>();
        queryWrapper.eq(SysAuthority::getSubjectId, id);
              queryWrapper.eq(SysAuthority::getSubjectType, subjectType.getValue())
                .eq(SysAuthority::getAuthorityType, authorityType.getValue());
       sysAuthorityMapper.delete(queryWrapper);
    }

    public List<SysAuthority> list(Long subjectId, SubjectTypeEnum subjectType, AuthorityTypeEnum authorityTypeEnum) {
        MPJLambdaWrapperX<SysAuthority> queryWrapper = new MPJLambdaWrapperX();
        queryWrapper
                .eq(SysAuthority::getSubjectType, EnumUtils.getValue(subjectType))
                .eq(SysAuthority::getSubjectId, subjectId)
                .eq(SysAuthority::getAuthorityType, EnumUtils.getValue(authorityTypeEnum));

        return sysAuthorityMapper.selectList(queryWrapper);
    }
}
